package net.chenlin.dp.modules.newBusiness.service.impl;

import lombok.extern.slf4j.Slf4j;
import net.chenlin.dp.common.entity.Page;
import net.chenlin.dp.common.entity.Query;
import net.chenlin.dp.common.entity.R;
import net.chenlin.dp.common.exception.RRException;
import net.chenlin.dp.common.utils.CommonUtils;
import net.chenlin.dp.common.utils.ExcelImportAnalysisUtils;
import net.chenlin.dp.modules.business.dao.DeviceRegisterMapper;
import net.chenlin.dp.modules.business.entity.po.ExceFilePo;
import net.chenlin.dp.modules.business.service.MinioSysFileService;
import net.chenlin.dp.modules.mqtt.SubscribeSample;
import net.chenlin.dp.modules.newBusiness.dao.DeviceCountMapper;
import net.chenlin.dp.modules.newBusiness.dao.NewDistrictManageMapper;
import net.chenlin.dp.modules.newBusiness.entity.dto.DistrictTopologyDto;
import net.chenlin.dp.modules.newBusiness.entity.dto.NewDistrictDto;
import net.chenlin.dp.modules.newBusiness.entity.po.DeviceUsersDataPo;
import net.chenlin.dp.modules.newBusiness.entity.po.DistrictTopologyPo;
import net.chenlin.dp.modules.newBusiness.entity.po.NewDistrictPo;
import net.chenlin.dp.modules.newBusiness.entity.po.NewExcelmportDataPo;
import net.chenlin.dp.modules.newBusiness.entity.vo.*;
import net.chenlin.dp.modules.newBusiness.service.NewDistrictManageService;
import net.chenlin.dp.modules.utils.TreeUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: Yansgm
 * @Description:
 * @Date: 2025-04-01 17:51:28
 */
@Service
@Slf4j
public class NewDistrictManageServiceImpl implements NewDistrictManageService {

    @Autowired
    private NewDistrictManageMapper mapper;

    @Autowired
    private SubscribeSample sample;

    @Autowired
    private DeviceCountMapper deviceCountMapper;

    @Autowired
    private DeviceRegisterMapper deviceRegisterMapper;

    @Autowired
    private MinioSysFileService sysFileService;

    @Override
    public R getAllDistrict(String name) {
        List<NewDistrictVo> list = mapper.getAllDistrict(name);
        list.stream().forEach(newDistrictVo -> {
            Integer offLine = mapper.getOffLinenNum(newDistrictVo.getId());
            if(offLine > 0){
                newDistrictVo.setMonitor(Boolean.TRUE);
            }else{
                newDistrictVo.setMonitor(Boolean.FALSE);
            }
            // 判断是否是线损异常
            newDistrictVo.setAlarm(Boolean.FALSE);
            // 获取台区下面的所有的设备ID
            List<DeviceCountVo> deviceCountVoList = deviceCountMapper.getAllDeviceId(newDistrictVo.getId());
            List<TopologyNodeTreeVo> nodeList = mapper.getTopologyNodeId(newDistrictVo.getId(),3);
            TopologyNodeTreeVo newTreeVo = new TopologyNodeTreeVo();
            for (DeviceCountVo deviceCountVo : deviceCountVoList){
                String deviceNum = deviceCountVo.getNodeName();
                // 获取当前设备下面的所有节点
                newTreeVo = deviceCountMapper.getTopologyNodeTreeVo(deviceNum);
                if(CollectionUtils.isNotEmpty(nodeList)){
                    if(newTreeVo != null){
                        addSubCompany(newTreeVo, nodeList);
                    }
                }

                BigDecimal usersCount = new BigDecimal(0.00);
                List<TopologyNodeTreeVo> childIds = TreeUtils.getChildIds(newTreeVo);
                List<TopologyNodeTreeVo> deviceList = childIds.stream().filter(treeVo -> treeVo.getNodeType() == 3).collect(Collectors.toList());
                // 计算每台监测设备关联的户表计量总和 = 该设备自身关联的所有户表的电量总和 + 该设备下级设备关联的户表电量总和（如果有多级关系，则逐级递归计算直至末级）
                for (TopologyNodeTreeVo treeVo : deviceList){
                    BigDecimal usersCount1 = deviceCountMapper.getUsersCount(treeVo.getDeviceAlias(), null, newDistrictVo.getId());
                    if(usersCount1 == null){
                        usersCount = usersCount.add(new BigDecimal(0.00));
                    }else {
                        usersCount = usersCount.add(usersCount1);
                    }
                }
                deviceCountVo.setUsersCount(usersCount);

                // 计算 - 监测设备计量结果
                BigDecimal deviceDayCount = new BigDecimal(0.00);
                BigDecimal querydeviceDayCount = deviceCountMapper.getDeviceDayCount(deviceNum,null);
                if(querydeviceDayCount != null ){
                    deviceDayCount = querydeviceDayCount;
                }

                if(deviceDayCount.compareTo(usersCount) > 0){
                    deviceCountVo.setLoss(deviceDayCount.subtract(usersCount));

                    BigDecimal result = deviceCountVo.getLoss().divide(deviceDayCount, 2, RoundingMode.HALF_UP).multiply(new BigDecimal(100));
                    String lineLossQualifiedTarget = newDistrictVo.getLineLossQualifiedTarget();
                    BigDecimal bigDecimal = new BigDecimal(lineLossQualifiedTarget);
                    if(bigDecimal.compareTo(result) != 1 && bigDecimal.compareTo(result) != 0 ){
                        //deviceCountVo.setIsAbnormal(Boolean.TRUE);
                        System.out.println("监控设备："+deviceCountVo.getNodeName()+"存在线损异常！！！");
                        newDistrictVo.setAlarm(Boolean.TRUE);
                    }
                }
            }
        });
        return CommonUtils.msg(list);
    }

    @Override
    public Page<NewDistrictListVo> getNewDistrictList(Map<String, Object> params) {
        Query query = new Query(params);
        Page<NewDistrictListVo> page = new Page<>(query);
        List<NewDistrictListVo> list = mapper.getNewDistrictList(page, query);
        // TODO: 2025/4/2 判断监控设备是否离线以及是否有线损异常并报警
        list.stream().forEach(newDistrictListVo -> {
            Integer offLine = mapper.getOffLinenNum(newDistrictListVo.getId());
            if(offLine > 0){
                newDistrictListVo.setMonitor(Boolean.TRUE);
            }else{
                newDistrictListVo.setMonitor(Boolean.FALSE);
            }
            // 判断是否是线损异常
            newDistrictListVo.setAlarm(Boolean.FALSE);
            // 获取台区下面的所有的设备ID
            List<DeviceCountVo> deviceCountVoList = deviceCountMapper.getAllDeviceId(newDistrictListVo.getId());
            List<TopologyNodeTreeVo> nodeList = mapper.getTopologyNodeId(newDistrictListVo.getId(),3);
            TopologyNodeTreeVo newTreeVo = new TopologyNodeTreeVo();
            for (DeviceCountVo deviceCountVo : deviceCountVoList){
                String deviceNum = deviceCountVo.getNodeName();
                // 获取当前设备下面的所有节点
                newTreeVo = deviceCountMapper.getTopologyNodeTreeVo(deviceNum);
                if(CollectionUtils.isNotEmpty(nodeList)){
                    if(newTreeVo != null){
                        addSubCompany(newTreeVo, nodeList);
                    }
                }

                // 计算 - 监测设备计量结果
                BigDecimal deviceDayCount = new BigDecimal(0.00);
                BigDecimal querydeviceDayCount = deviceCountMapper.getDeviceDayCount(deviceNum,null);
                if(querydeviceDayCount != null ){
                    deviceDayCount = querydeviceDayCount;
                }


                // 计算 - 户表计量结果
                BigDecimal usersCount = new BigDecimal(0.00);
                List<TopologyNodeTreeVo> childIds = TreeUtils.getChildIds(newTreeVo);
                List<TopologyNodeTreeVo> deviceList = childIds.stream().filter(treeVo -> treeVo.getNodeType() == 3).collect(Collectors.toList());
                // 计算每台监测设备关联的户表计量总和 = 该设备自身关联的所有户表的电量总和 + 该设备下级设备关联的户表电量总和（如果有多级关系，则逐级递归计算直至末级）
                for (TopologyNodeTreeVo treeVo : deviceList){
                    BigDecimal usersCount1 = deviceCountMapper.getUsersCount(treeVo.getDeviceAlias(), null, newDistrictListVo.getId());
                    if(usersCount1 == null){
                        usersCount = usersCount.add(new BigDecimal(0.00));
                    }else {
                        usersCount = usersCount.add(usersCount1);
                    }
                }
                deviceCountVo.setUsersCount(usersCount);

                //
                if(deviceDayCount.compareTo(usersCount) > 0){
                    deviceCountVo.setLoss(deviceDayCount.subtract(usersCount));

                    BigDecimal result = deviceCountVo.getLoss().divide(deviceDayCount, 2, RoundingMode.HALF_UP).multiply(new BigDecimal(100));
                    String lineLossQualifiedTarget = newDistrictListVo.getLineLossQualifiedTarget();
                    BigDecimal bigDecimal = new BigDecimal(lineLossQualifiedTarget);
                    if(bigDecimal.compareTo(result) != 1 && bigDecimal.compareTo(result) != 0 ){
                        //deviceCountVo.setIsAbnormal(Boolean.TRUE);
                        System.out.println("监控设备："+deviceCountVo.getNodeName()+"存在线损异常！！！");
                        newDistrictListVo.setAlarm(Boolean.TRUE);
                    }
                }
            }
        });
        return page;
    }

    @Override
    @Transactional
    public R creatDistrict(NewDistrictDto dto) {
        NewDistrictPo districtPo = new NewDistrictPo();
        BeanUtils.copyProperties(dto,districtPo);
        mapper.insetDistrict(districtPo);
        // 默认创建一级台区变压器节点
        DistrictTopologyPo po = new DistrictTopologyPo();
        po.setNodeName("台区变压器");
        po.setDeviceAlias("台区变压器");
        po.setNodeType(0);
        po.setParentNodeId(0);
        po.setDistrictId(districtPo.getId());
        po.setCreateId(dto.getCreateId());

        mapper.insertTopologyNode(po);
        return R.ok();
    }

    @Override
    public R editDistrict(NewDistrictDto dto) {
        NewDistrictPo districtPo = new NewDistrictPo();
        BeanUtils.copyProperties(dto,districtPo);
        mapper.updateDistrict(districtPo);
        return R.ok();
    }

    @Override
    public R deleteDistrict(Integer id) {
        mapper.deleteDistrict(id);
        // 删除台区下面的节点以及设备信息
        mapper.deleteDistrictNode(id);
        return R.ok();
    }

    @Override
    @Transactional
    public R creatTopologyNode(DistrictTopologyDto dto) {
        DistrictTopologyPo po = new DistrictTopologyPo();
        BeanUtils.copyProperties(dto,po);
       if(dto.getNodeType() == 3){
           TopologyNodeVo onlineNum = mapper.getOnlineNodeByName(dto.getNodeName());
           if(onlineNum != null){
               return R.error("设备编号"+dto.getNodeName()+"已安装并在线，请勿重复创建监测设备！");
               //throw new RRException("设备编号"+dto.getNodeName()+"已安装并在线，请勿重复创建监测设备！");
           }
           po.setOnline(1);
           po.setOnlineTime(new Date());
       }
        mapper.insertTopologyNode(po);
        if(dto.getNextNodeId()!= null){
            // 修改下级直接点的父级ID
            mapper.updateNodeParentId(po.getNodeId(),dto.getNextNodeId());
        }
        if(dto.getNodeType() == 3){
            // 先监听回复消息（查询消息在获取最新数据的时候查询，这里只监听数据消息）
            //String replyClient = "reply_client";
            //String replyTopic = "monitor/"+dto.getNodeName()+"/query_reply";
            //sample.start(replyClient,replyTopic);

            //System.out.println("订阅方已完成监听！！");

            // 发送查询的topic
            //String topic = "monitor/"+dto.getNodeName()+"/query";
            //PublishSample.sendMessage(topic);

            // 监听数据消息
            String dateclientid = dto.getNodeName()+"_data_client";
            String dateTopic = "monitor/"+dto.getNodeName()+"/data";
            sample.start(dateclientid,dateTopic);
        }
//        if(dto.getNodeType() == 3){
//            Thread.sleep(1000); // 参数是毫秒
//            TopologyNodeVo vo = mapper.getTopologNodeByName(dto.getNodeName());
//            System.out.println("vo.getOnline() == "+vo.getOnline());
//            if(vo.getOnline() != null){
//                if(vo.getOnline() == 1){
//                    // 监听数据消息
//                    String dateTopic = "monitor/"+dto.getNodeName()+"/data";
//                    sample.start(dateTopic);
//                }
//            }
//        }
        return R.ok();
    }

    @Override
    @Transactional
    public R editTopologyNode(DistrictTopologyDto dto) {
        DistrictTopologyPo po = new DistrictTopologyPo();
        BeanUtils.copyProperties(dto,po);
        mapper.editTopologyNode(po);
        if(dto.getNextNodeId()!= null){
            // 修改下级直接点的父级ID
            mapper.updateNodeParentId(po.getNodeId(),dto.getNextNodeId());
        }
        return R.ok();
    }

    @Override
    @Transactional
    public R deleteTopologyNode(Integer nodeId) {
        // 获取当前节点的父级ID
        TopologyNodeVo nodeVo = mapper.getTopologyNode(nodeId);
        mapper.deleteTopologyNode(nodeId);
        List<TopologyNodeVo> list = mapper.getNextTopologNode(nodeId);
        if(CollectionUtils.isNotEmpty(list)){
            list.stream().forEach(vo -> {
                // 修改当前删除节点的下级子节点的父级为删除节点的父级
                mapper.updateNodeParentId(nodeVo.getParentNodeId(),vo.getNodeId());
            });
        }
        return R.ok();
    }

    @Override
    public R getNextTopologNode(Integer nodeId) {
        List<TopologyNodeVo> list = mapper.getNextTopologNode(nodeId);
        return CommonUtils.msg(list);
    }

    @Override
    public R getTree(Integer districtId) {
        List<TopologyNodeTreeVo> treeVoList = new ArrayList<>();
        List<TopologyNodeTreeVo> list = mapper.getTopologyNodeId(districtId,null);
        NewDistrictVo vo = mapper.getDistrictInfo(districtId);
//        if(CollectionUtils.isEmpty(list)){
//            TopologyNodeTreeVo treeVo = new TopologyNodeTreeVo();
//            treeVo.setDistrictId(districtId);
//            treeVo.setNodeId(districtId);
//            treeVo.setNodeName(vo.getName());
//            return CommonUtils.msg(treeVoList.add(treeVo));
//        }
        list.stream().forEach(treeVo -> {
            if(StringUtils.isEmpty(treeVo.getParentNodeName())){
                treeVo.setParentNodeName(vo.getName());
                treeVo.setParentNodeType("台区");
            }
            if(treeVo.getCircuitType() != null){
                if(treeVo.getCircuitType() == 0){
                    treeVo.setCircuitTypeName("三相电路");
                }
                if(treeVo.getCircuitType() == 1){
                    treeVo.setCircuitTypeName("单相电路");
                }
            }
        });
        List<TopologyNodeTreeVo> topList = list.stream().filter(treeVo -> treeVo.getParentNodeId() == 0).collect(Collectors.toList());
        if(CollectionUtils.isNotEmpty(topList)){
            topList.stream().forEach(topListVo ->{
                TopologyNodeTreeVo treeVo = new TopologyNodeTreeVo();
                if(CollectionUtils.isNotEmpty(list)){
                    treeVo = topListVo;
                    addSubCompany(treeVo, list);
                }
                treeVoList.add(treeVo);
            });
        }
        return CommonUtils.msg(treeVoList);
    }

    @Override
    public R getTopologyList(Integer districtId) {
        List<TopologyListVo> list = mapper.getTopologyList(districtId);
        NewDistrictVo vo = mapper.getDistrictInfo(districtId);
        list.stream().forEach(topologyListVo -> {
            if(StringUtils.isEmpty(topologyListVo.getParentNodeName())){
                topologyListVo.setParentNodeName(vo.getName());
                topologyListVo.setParentNodeType("台区");
            }
        });
        return CommonUtils.msg(list);
    }

    @Override
    public R getDistrictInfo(Integer id) {
        NewDistrictVo vo = mapper.getDistrictInfo(id);
        // 查询台区用电情况
        ElectricityUseInfo electricityUse = new ElectricityUseInfo();
        // 获取监测设备的电量和
        // 获取台区下面所有的监测设备
        // 获取台区下面的所有的设备ID
        List<DeviceCountVo> list = deviceCountMapper.getAllDeviceId(id);
        List<TopologyNodeTreeVo> nodeList = mapper.getTopologyNodeId(id,3);
        TopologyNodeTreeVo newTreeVo = new TopologyNodeTreeVo();
        Integer usersNum = 0;
        BigDecimal powerSupply = new BigDecimal(0.00);
        for (DeviceCountVo deviceCountVo : list){
            String deviceNum = deviceCountVo.getNodeName();
            // 获取当前设备下面的所有节点
            newTreeVo = deviceCountMapper.getTopologyNodeTreeVo(deviceNum);
            if(CollectionUtils.isNotEmpty(nodeList)){
                addSubCompany(newTreeVo, nodeList);
            }

            List<TopologyNodeTreeVo> childIds = TreeUtils.getChildIds(newTreeVo);

            // 计算监测设备的关联用户数 = 该设备自身直接关联的用户数+该设备下级设备关联的用户数（如果有多级关系，则逐级递归计算直至末级）

            List<TopologyNodeTreeVo> deviceList = childIds.stream().filter(treeVo -> treeVo.getNodeType() == 3).collect(Collectors.toList());
            if(CollectionUtils.isNotEmpty(deviceList)){
                Integer sum = deviceList.stream().filter(item -> item.getUserNum() != null).mapToInt(TopologyNodeTreeVo::getUserNum).sum();
                System.out.println("sum === "+sum);
                usersNum = usersNum + sum;
            }

            // 获取当前设备下面的所有节点
            newTreeVo = deviceCountMapper.getTopologyNodeTreeVo(deviceNum);
            if(CollectionUtils.isNotEmpty(nodeList)){
                if(newTreeVo != null){
                    addSubCompany(newTreeVo, nodeList);
                }
            }

            // 计算 - 监测设备计量结果
            BigDecimal deviceDayCount = new BigDecimal(0.00);

            BigDecimal querydeviceDayCount = deviceCountMapper.getDeviceDayCount(deviceNum,null);
            if(querydeviceDayCount != null ){
                deviceDayCount = querydeviceDayCount;
            }

            BigDecimal usersCount = new BigDecimal(0.00);

            // 计算每台监测设备关联的户表计量总和 = 该设备自身关联的所有户表的电量总和 + 该设备下级设备关联的户表电量总和（如果有多级关系，则逐级递归计算直至末级）
            for (TopologyNodeTreeVo treeVo : deviceList){
                BigDecimal usersCount1 = deviceCountMapper.getUsersCount(treeVo.getDeviceAlias(), null, id);
                if(usersCount1 == null){
                    usersCount = usersCount.add(new BigDecimal(0.00));
                }else {
                    usersCount = usersCount.add(usersCount1);
                }
            }
            deviceCountVo.setUsersCount(usersCount);
            deviceCountVo.setUsersCount(usersCount);
            if(deviceDayCount.compareTo(usersCount) > 0){
                deviceCountVo.setLoss(deviceDayCount.subtract(usersCount));

                BigDecimal result = deviceCountVo.getLoss().divide(deviceDayCount, 2, RoundingMode.HALF_UP).multiply(new BigDecimal(100));
                String lineLossQualifiedTarget = vo.getLineLossQualifiedTarget();
                BigDecimal bigDecimal = new BigDecimal(lineLossQualifiedTarget);
                if(bigDecimal.compareTo(result) != 1 && bigDecimal.compareTo(result) != 0 ){
                    //deviceCountVo.setIsAbnormal(Boolean.TRUE);
                    System.out.println("监控设备："+deviceCountVo.getNodeName()+"存在线损异常！！！");
                    vo.setAlarm(Boolean.TRUE);
                }
            }

            BigDecimal count = mapper.getDeviceElectricity(deviceNum);
            if(count != null){
                powerSupply = powerSupply.add(count);
            }
        }
        vo.setUserNum(usersNum);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年-MM月-dd日");
        String dateString = sdf.format(new Date());
        electricityUse.setDatetime(dateString);
        electricityUse.setPowerSupply(powerSupply);
        vo.setElectricityUse(electricityUse);

        Integer offLine = mapper.getOffLinenNum(id);
        if(offLine > 0){
            vo.setMonitor(Boolean.TRUE);
        }else{
            vo.setMonitor(Boolean.FALSE);
        }
        // 判断是否是线损异常
        vo.setAlarm(Boolean.FALSE);
        return CommonUtils.msg(vo);
    }

    @Override
    public void deviceExcel(MultipartFile file,Long districtId) throws Exception {
        if (Objects.isNull(file) || file.isEmpty()) {
            throw new RRException("导入文件为空");
        }
        String filename = file.getOriginalFilename();
        if (filename == null) {
            throw new RRException("文件名为空");
        }
        String url = sysFileService.uploadExcelFile(file);
        ExceFilePo filePo = new ExceFilePo();
        filePo.setFileName(filename);
        filePo.setImportTime(new Date());
        List<DeviceUsersDataPo> deviceUsersDataPos = ExcelImportAnalysisUtils.deviceUsersExcelParseRowCell(filename, file.getInputStream(), 0);
        if(CollectionUtils.isNotEmpty(deviceUsersDataPos)){
            deviceUsersDataPos.stream().forEach(deviceUsersDataPo -> {
                deviceUsersDataPo.setDistrictId(districtId);
            });
            mapper.insertDeviceUsersData(deviceUsersDataPos);
        }
        filePo.setFileUrl(url);
        deviceRegisterMapper.insertExcelImportInfo(filePo);
    }

    @Override
    public Page<DeviceUsersDataVo> getDeviceUserList(Map<String, Object> params) {
        Query query = new Query(params);
        Page<DeviceUsersDataVo> page = new Page<>(query);
        List<DeviceUsersDataVo> list = mapper.getDeviceUserList(page, query);
        return page;
    }

    @Override
    public R getDistrictOnline(Integer id) {
        Integer onlinNum = mapper.getDistrictOnline(id);
        if(onlinNum > 0){
            return R.error("系统显示该台区仍有设备在运行，确定要删除该台区？");
        }
        return R.ok();
    }

    public static void main(String[] args) throws ParseException {
        String  me = "2023/4/17 1:15:00";
        Date writeTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(me);
        System.out.println(writeTime);
    }

    @Override
    public void importUserData(MultipartFile file,Long districtId) throws Exception {
        if (Objects.isNull(file) || file.isEmpty()) {
            throw new RRException("导入文件为空");
        }
        String filename = file.getOriginalFilename();
        if (filename == null) {
            throw new RRException("文件名为空");
        }
        String url = sysFileService.uploadExcelFile(file);
        ExceFilePo filePo = new ExceFilePo();
        filePo.setFileName(filename);
        filePo.setImportTime(new Date());
        // 调用解析文件方法
        List<NewExcelmportDataPo> list = ExcelImportAnalysisUtils.userDataExcelParseRowCell(filename, file.getInputStream(), 0);
        if(CollectionUtils.isNotEmpty(list)){
            list.stream().forEach(newExcelmportDataPo -> {
                newExcelmportDataPo.setDistrictId(districtId);
            });
            mapper.batchInsertData(list);
            Date writeTime = list.get(0).getWriteTime();
            String format = new SimpleDateFormat("yyyy-MM-dd").format(writeTime);
            filePo.setImportMeteringTime(format);
        }
        filePo.setFileUrl(url);
        deviceRegisterMapper.insertExcelImportInfo(filePo);
    }

    public static TopologyNodeTreeVo addSubCompany(TopologyNodeTreeVo company, List<TopologyNodeTreeVo> data) {
        //构造一个子公司列表
        List<TopologyNodeTreeVo> subCompany = new ArrayList<>();
        for (TopologyNodeTreeVo item : data) {//遍历数据
            //如果遍历到的部门是当前部门的子部门
            if (item.getParentNodeId().equals(company.getNodeId())) {
                //把遍历到的公司添加到子公司列表，并且递归地调用本方法
                TopologyNodeTreeVo topologyNodeTreeVo = addSubCompany(item, data);
                subCompany.add(topologyNodeTreeVo);
            }
        }
        company.setChild(subCompany);
        return company;
    }
}
